﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShopOnline.Models.DAL
{
    public class DAL_Press
    {
       
        private ShopOnline.Models.ShopOnlineDBDataContext SC_DB;
        public DAL_Press()
        {
            SC_DB = new ShopOnlineDBDataContext();
        }


        public List<Press> getListPress()
        {
            return SC_DB.Presses.OrderBy(a => a.pressId).ToList();
        }
        public List<Press> getListPressShow()
        {
            var list = (from c in SC_DB.Presses where c.show == true orderby c.piority,c.pressId select c).ToList();
            return list;
        }

        public Press getPressById(int PressId)
        {
            return (from c in SC_DB.Presses where c.pressId==PressId select c).FirstOrDefault();
        }

        public int CreatePress(Press press)
        {
            int result = 0;
            try
            {
                SC_DB.Presses.InsertOnSubmit(press);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {
                
            }
            return result;
        }

        public int EditCPress(Press pre)
        {
            int result = 0;
            try
            {
                var press = (from c in SC_DB.Presses where c.pressId == pre.pressId select c).FirstOrDefault();
                press.body = pre.body;
                press.curent = pre.curent;
                press.dateCreate = pre.dateCreate;
                press.description = pre.description;
                press.imagePath = pre.imagePath;
                press.show = pre.show;
                press.userCreate = pre.userCreate;
                press.viewCount = pre.viewCount;
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }
        public int DeletePress(Press press)
        {
            int result = 0;
            try
            {
                var Press = (from c in SC_DB.Presses where c.pressId == press.pressId select c).FirstOrDefault();
                SC_DB.Presses.DeleteOnSubmit(press);
                SC_DB.SubmitChanges();
                result = 1;
            }
            catch
            {

            }
            return result;
        }

        public List<Press> getTopnPress(int n)
        {
            return (from pre in SC_DB.Presses orderby pre.piority, pre.pressId descending where pre.show == true select pre).Skip(0).Take(n).ToList(); 
        }

       public List<Press> get6PressPaging( int n,int skip){
            
               int catelogyIdDefault = (from p in SC_DB.Catelogies orderby p.catelogyId select p.catelogyId).First();

               return (from pre in SC_DB.Presses orderby pre.pressId descending where pre.show == true select pre).Skip(n ).Take(skip).ToList();
        }

       public List<Press> getlistPreeBypress(int pressId)
       {
           Press pre = getPressById(pressId);
           List<Press> list = (from p in SC_DB.Presses orderby p.dateCreate where (p.show == true && p.dateCreate >= pre.dateCreate) select p).Take(8).ToList();
           return list;
       }
       public List<Press> getListTop2Press()
       {
           var listtop2press = (from c in SC_DB.Presses orderby c.dateCreate where (c.show == true) select c).Take(2).ToList();
           return listtop2press;
       }
    }
}